package flc.ast.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileActivity;
import flc.ast.activity.PlayActivity;
import flc.ast.adapter.VideoAdapter;
import flc.ast.bean.MyFileBean;
import flc.ast.databinding.FragmentPlayerBinding;
import flc.ast.dialog.AddDialog;
import flc.ast.dialog.TypeDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.UriUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayerFragment extends BaseNoModelFragment<FragmentPlayerBinding> {

    private VideoAdapter videoAdapter;
    private List<MyFileBean> listShow = new ArrayList<>();//全部数据

    @Override
    protected int onCreate() {
        return R.layout.fragment_player;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.llMyFile.setOnClickListener(this);
        mDataBinding.ivEdit.setOnClickListener(this);
        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext));
        videoAdapter = new VideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llMyFile://我的文件
                FileActivity.path = Content.myFile;
                startActivity(FileActivity.class);
                break;
            case R.id.ivAdd://添加
                showTypeDialog();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PlayActivity.videoUri = videoAdapter.getItem(position).getPath();
        startActivity(PlayActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getFolderData();
        getData();
    }

    private void getFolderData() {
        List<String> listData = new ArrayList<>();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listData.add(file.getPath());
            }
        }
        mDataBinding.tvFileSize.setText(getString(R.string.file_size_num, listData.size()));
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myVideo;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyFileBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            videoAdapter.setList(listShow);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        goToSel();
                    }
                }).request();
    }

    private void goToSel() {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.VIDEO)
                .minCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        for (SelectMediaEntity entity : data) {
                            String filePath = FileUtil.generateFilePath(Content.myVideo, "." + FileUtils.getFileExtension(entity.getPath()));
                            FileUtils.copy(entity.getPath(), filePath);
                        }
                        getData();
                        ToastUtils.showShort(getString(R.string.add_suc));
                    }
                });
    }

    private void showTypeDialog() {
        TypeDialog dialog = new TypeDialog(mContext);
        dialog.setListener(new TypeDialog.Ilistener() {

            @Override
            public void clickFolder() {
                showAddDialog();
            }

            @Override
            public void clickVideo() {
                getPermission();
            }
        });
        dialog.show();
    }

    private void showAddDialog() {
        AddDialog dialog = new AddDialog(mContext);
        dialog.setListener(new AddDialog.Ilistener() {
            @Override
            public void clickAddFolder(String name) {
                String path = PathUtils.getFilesPathExternalFirst() + Content.myFile + "/" + name;
                if (FileUtils.isFileExists(path)) {
                    ToastUtils.showShort(getString(R.string.folder_is_exist));
                } else {
                    FileUtils.createOrExistsDir(path);
                    ToastUtils.showShort(getString(R.string.add_folder_suc));
                }
                getFolderData();
            }
        });
        dialog.show();
    }

}